package cn.worken.baseframe.api.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.function.Predicate;

/**
 * @author genxin.liu
 * @since 2021/10/22
 */
@Import(SwaggerUiWebMvcConfigurer.class)
@Configuration
public class SwaggerConfiguration {
    
    @Bean
    public Docket testApi(@Value("${swagger.enable:false}") boolean swaggerEnable) {
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(swaggerEnable)
                .apiInfo(apiInfo())
                .select()
                .apis(testApis())
                .build();
    }
    
    private Predicate<RequestHandler> testApis() {
        return RequestHandlerSelectors.basePackage("cn.worken.baseframe.api.controller");
    }
    
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("base-frame")
                .version("1.0-SNAPSHOT")
                .build();
    }
    
}
